package linkwechat.service.systemservice;

import cn.hutool.core.date.DateTime;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import linkwechat.mapper.system.RoleMapper;
import linkwechat.pojo.LinkMenu;
import linkwechat.pojo.LinkRole;
import linkwechat.pojo.ReturnData;
import linkwechat.pojo.ReturnEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleGlService {
    @Autowired
    RoleMapper roleMapper;

    public ReturnData getRole(LinkRole role){
        PageHelper.startPage(role.getPage(),role.getSize());
        List<LinkRole> allrole = roleMapper.GetRole(role);
        PageInfo<LinkRole> linkRolePageInfo = PageInfo.of(allrole);
        return ReturnData.builder().code(ReturnEnum.QUERY_SUCCEED.getCode()).msg(ReturnEnum.QUERY_SUCCEED.getMsg()).data(linkRolePageInfo).build();
    }

    public ReturnData SelectMenu() {
        return ReturnData.builder()
                .msg(ReturnEnum.QUERY_SUCCEED.getMsg())
                .code(ReturnEnum.QUERY_SUCCEED.getCode())
                .data(roleMapper.getMenu())
                .build();
    }

    //新增权限
    public ReturnData AddRole(LinkRole role){
        Integer i1 = 0;
        role.setCreateTime(DateTime.now());
        List<LinkMenu> linkMenus = JSON.parseArray(role.getMenus(), LinkMenu.class);
        Integer i = roleMapper.AddRole(role);
        if (i>0){
            Long roleId = role.getRoleId();
            if (linkMenus.size()<=0){
                return ReturnData.builder().code(ReturnEnum.APPEND_SUCCEED.getCode()).msg(ReturnEnum.APPEND_SUCCEED.getMsg()).data(i).build();
            }else {
                for (LinkMenu linkMenu : linkMenus) {
                    i1 = roleMapper.Add_Role_Menu(roleId, linkMenu.getMenuId());
                }
                if (i1 > 0) {
                    return ReturnData.builder().code(ReturnEnum.APPEND_SUCCEED.getCode()).msg(ReturnEnum.APPEND_SUCCEED.getMsg()).data(i + i1).build();
                }
            }
        }
        return ReturnData.builder().data(0).build();
    }

    public ReturnData SelectMenuRole(Long id) {
        return ReturnData.builder()
                .code(ReturnEnum.QUERY_SUCCEED.getCode())
                .msg(ReturnEnum.QUERY_SUCCEED.getMsg())
                .data(roleMapper.selectMenuRole(id))
                .build();
    }

    public ReturnData UpdRole(LinkRole role) {
        Integer i1 = 0;
        //修改角色
         role.setUpdateTime(DateTime.now());
        Integer i = roleMapper.UpdRole(role);
        List<LinkMenu> linkMenus = JSON.parseArray(role.getMenus(), LinkMenu.class);
        if (i>0){
            //修改权限
            roleMapper.DelRoleMenu(role.getRoleId());
            if (linkMenus.size()>0){
                for (LinkMenu linkMenu : linkMenus) {
                     i1 += roleMapper.Add_Role_Menu(role.getRoleId(), linkMenu.getMenuId());
                }
            }
        }
        if (i1 == linkMenus.size()){
            return ReturnData
                    .builder()
                    .data(i1+i)
                    .msg(ReturnEnum.MODIFY_SUCCEED.getMsg())
                    .code(ReturnEnum.MODIFY_SUCCEED.getCode())
                    .build();
        }

        return ReturnData
                .builder()
                .data(0)
                .build();
    }

    public ReturnData DeleteRole(Long[] ids) {
        Integer i  = 0;
        for (Long id : ids) {
            i += roleMapper.DelRoles(id);
        }
        if (i==ids.length){
            return ReturnData
                    .builder()
                    .code(ReturnEnum.REMOVE_SUCCEED.getCode())
                    .msg(ReturnEnum.REMOVE_SUCCEED.getMsg())
                    .data(i)
                    .build();
        }
        return ReturnData.builder().data(0).build();
    }
}
